package email;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class MainDialog extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3548132391803440630L;
	
    private FileManager fileManager;
	private JTable table;
	private JPopupMenu popupMenu;
	
	private Object[][] tableData = {};

	private Object[] columnTitle = {"文件名" , "大小"};
	
	public MainDialog() {
		super();
		
		fileManager = new FileManager(this);
	}
	
	public FileManager getFileManager() {
		return fileManager;
	}

	public void setFileManager(FileManager fileManager) {
		this.fileManager = fileManager;
	}

	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}

	public JPopupMenu getPopupMenu() {
		return popupMenu;
	}

	public void setPopupMenu(JPopupMenu popupMenu) {
		this.popupMenu = popupMenu;
	}

	public void display(Object[][] dataSource) {
		setTitle("My Drawer");
		int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
		int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
        setBounds((screenWidth - 500) / 2, (screenHeight - 500) / 2, 500, 500);
		table = new JTable(dataSource, columnTitle) {
			public boolean isCellEditable(int row, int column) {
				return false;// 父类的方法里面是 return true的，所以就可以编辑了~~~
			}
		};
		// 添加下面的代码后，不能用鼠标选中多行。
		//table.setEnabled(false);
		table.addMouseListener(new UserMouseOperation(this));
		
		JScrollPane scrollPane = new JScrollPane();   //支持滚动
	    getContentPane().add(scrollPane,BorderLayout.CENTER);
	    //table.setRowSelectionInterval(1, 3); //设置选择中行
        //table.addRowSelectionInterval(5, 5);  //追加
        
	    scrollPane.setViewportView(table);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	public void createPopupMenu(int x, int y) {
		popupMenu = new JPopupMenu();
		
		JMenuItem sendMailMenuItem = new JMenuItem();
		sendMailMenuItem.setText(" 上传 ");
		sendMailMenuItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				// 将文件以附件的形式发送出去
				int [] selectRows = table.getSelectedRows();  //获得所有被选中的索引
		        System.out.println("以下为所有被选中的行:");
		        for(int i=0;i<selectRows.length;i++){
		            String filename = String.valueOf(table.getValueAt(selectRows[i], 0));
		            System.out.println(filename);
		        }
			}
		});
		popupMenu.add(sendMailMenuItem);
		popupMenu.show(this, x, y);
	}
	
	
}
